package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.MovieRepository;
import com.example.moviecms2.utils.ResultVOUtils;
import com.example.moviecms2.vo.MovieVo;
import com.example.moviecms2.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/movie")
public class MovieRestController {

    @Autowired
    private MovieRepository movieRepository;


        @RequestMapping(value = "/lists",method = RequestMethod.GET)
    public ResultVo<List<MovieVo>> lists(){
        List<Movie> list = movieRepository.findAll();

        List<MovieVo> volist = new ArrayList<>();

        //遍历movie的list 并赋值给MovieVo  然后在添加到volist的集合里
        for (Movie movie: list) {
            MovieVo vo = new MovieVo();
            vo.setId(movie.getId());
            vo.setName(movie.getName());
            vo.setPhoto(movie.getPhoto());

            volist.add(vo);
        }

//        ResultVo resultVo = new ResultVo();
//        resultVo.setCode(200);
//        resultVo.setMsg("success");
//        resultVo.setData(volist);
//        return resultVo;

            return ResultVOUtils.success(volist);
    }







    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultVo<MovieVo> doAdd(@RequestParam String name, @RequestParam String photo, @RequestParam String createDate){
        //重定向（浏览器），请求转发（服务器）
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        Movie result = movieRepository.save(movie);

        //为空调用工具类success方法,不为空调用fail方法
        if (result !=null){

            return ResultVOUtils.success(new MovieVo(result));
        }else {
            return ResultVOUtils.fail();
        }


    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResultVo<MovieVo> detail(Model model,@PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        MovieVo vo = new MovieVo(movie);

//        ResultVo resultVo = new ResultVo();
//        resultVo.setCode(200);
//        resultVo.setMsg("success");
//        resultVo.setData(vo);
//        return resultVo;

        return ResultVOUtils.success(vo);

    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public String doEdit(@PathVariable  Long id,@RequestParam  String name,@RequestParam  String photo,@RequestParam String createDate){

        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
            date = format.parse(createDate);
        }catch (Exception e){

        }
        //1.通过id获取数据库的movie对象
        Movie movie = movieRepository.findOne(id);
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        //2.通过调用数据访问接口更新数据
        Movie result = movieRepository.save(movie);
        return result ==null ?"fail":"success" ;
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResultVo<String> delete(@PathVariable Long id){
        movieRepository.delete(id);

//        ResultVo<String> vo = new ResultVo<>();
//        vo.setCode(200);
//        vo.setMsg("success");
//        vo.setData(null);
//        return vo;
        return ResultVOUtils.success();
    }


}
